package com.gps.web.dao.sheet;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gps.web.comm.dao.BaseDaoBean;
import com.gps.web.entity.sheets.Contact;

@Repository
public class ContactDaoBean extends BaseDaoBean<Contact, Integer> implements
		ContactDao {

	@Override
	@Transactional
	public List<Contact> getContactByPage(Map<String, Object> conditions) {
		return findByCriteria(getCriteria(conditions));
	}
	
	private Criteria getCriteria(Map<String, Object> conditions) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(Contact.class);

		if(!StringUtils.isBlank((String)conditions.get("fullName"))){
			criteria.add(Restrictions.eq("fullName", conditions.get("fullName").toString()));
		}
		if(!StringUtils.isBlank((String)conditions.get("phone"))){
			criteria.add(Restrictions.eq("phone", conditions.get("phone").toString()));
		}
		if(!StringUtils.isBlank((String)conditions.get("workUnit"))){
			criteria.add(Restrictions.like("workUnit", conditions.get("workUnit").toString(), MatchMode.ANYWHERE));
		}
		
		return criteria;
	}
}
